<!-- 商学院视频信息 -->
<template>
  <div class="table">
    <el-tabs v-model="activeName" @tab-click="handleChangeTabs">
      <el-tab-pane label="视频列表" name="one">
        <SearchTable ref="searchTableRef" :selectEntityList="selectEntityList" v-model:formStatus="formStatus" @handleAdd="handleAdd" @handleViewEdit="handleViewEdit" />
      </el-tab-pane>
      <el-tab-pane label="视频详情" name="two">
        <FormTab :selectEntityList="selectEntityList" :whetherStatue="whetherStatue" v-model:formStatus="formStatus" v-model:form="form" @handleInitData="clearData" @handleSuccess="handleSuccess" @handleEntityRefresh="handleGetBusinessSchoolEntityInfo" />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script setup lang="ts" name="businessSchoolsVideoInfo">
import SearchTable from "@/components/businessSchoolManager/businessSchoolVideo/SearchTable.vue";
import FormTab from "@/components/businessSchoolManager/businessSchoolVideo/FormTab.vue";
import type { TabsMode, ViewMode, ViewModeFilter } from "@/types/common";
import type { TabsPaneContext } from "element-plus";
import type { ISchoolVideo, ISchoolVideoDTO } from "@/types/businessSchoolManager/businessSchoolVideo";
import { getBusinessSchoolEntityInfo } from "@/utils/api/businessSchoolEntity";
import type { ISchoolEntity, ISearchSchoolEntity } from "@/types/businessSchoolManager/businessSchoolEntity";
import { useDictData } from "@/hooks/network/useDictData";
import type { IDictData } from "@/types/dict";
let { getDictData } = useDictData();
/* 是否信息 */
const whetherStatue = ref<Array<IDictData>>([]);

/* 表格状态 */
const formStatus = ref<ViewMode>("none");
/* 标签页切换 */
const activeName = ref<TabsMode>("one");
const selectEntityList = ref<Array<ISchoolEntity>>([]);
/* 表格子组件的引用 */
const searchTableRef = useTemplateRef("searchTableRef");

const form = reactive<ISchoolVideoDTO>({
  id: 0,
  is_show: "",
  image: "",
  title: "",
  business_school_entity_id: "",
  video: "",
  sort: 0,
  length: ""
});

/* 处理切换标签页 */
function handleChangeTabs(tab: TabsPaneContext) {
  if (tab.props.name === "two") {
    formStatus.value = "edit";
  } else if (tab.props.name === "one") {
    clearData();
  }
}

/* 清除数据 */
function clearData() {
  activeName.value = "one";
  formStatus.value = "none";
}

/* 处理修改成功 */
function handleSuccess() {
  searchTableRef.value?.getTableInfo();
}

/* 点击添加按钮 */
function handleAdd(code: ViewMode) {
  formStatus.value = code;
  activeName.value = "two";
}
/* 点击编辑按钮 */
function handleViewEdit(row: ISchoolVideo, code: ViewModeFilter) {
  formStatus.value = code;
  activeName.value = "two";
  /* nextTick 解决重置该表单项 初始化的数据问题 */
  nextTick(() => {
    form.id = row.id;
    form.business_school_entity_id = row.business_school_entity_id;
    form.image = row.image;
    form.title = row.title;
    form.video = row.video;
    form.sort = row.sort;
    form.is_show = row.is_show;
    form.length = row.length;
  });
}

/* 获取发布主体 */
function handleGetBusinessSchoolEntityInfo() {
  getBusinessSchoolEntityInfo<Array<ISchoolEntity>, ISearchSchoolEntity>()
    .then(res => {
      selectEntityList.value = res.result;
    })
    .finally(() => {});
}

handleGetBusinessSchoolEntityInfo();
getDictData("WHETHER", whetherStatue);
</script>

<style lang="scss" scoped></style>
